package com.sunwayworld.basemodule.business.report.validators;

import com.sunwayworld.basemodule.business.report.bean.LimsReportBean;
import com.sunwayworld.basemodule.samplemanage.samplehandle.bean.LimsSampleHandleBean;
import com.sunwayworld.basemodule.samplemanage.samplehandle.service.LimsSampleHandleService;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.validator.data.DataValidator;
import com.sunwayworld.module.item.file.bean.CoreFileBean;
import com.sunwayworld.module.item.file.service.CoreFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsReportSubmitValidator implements DataValidator {

    @Autowired
    private CoreFileService coreFileService;

    @Override
    public boolean doValid(Object... args) {
        RestJsonWrapperBean wrapper = (RestJsonWrapperBean) args[0];
        List<LimsReportBean> reportBeans = wrapper.parse(LimsReportBean.class);
        List<Long> reportIds = reportBeans.stream().map(LimsReportBean::getId).collect(Collectors.toList());
        List<CoreFileBean> atts = coreFileService.selectFileListByTargetIds(reportIds.stream().map(s -> "T_LIMS_REPORT$" + s).toArray(String[]::new)); // 报告附件

        if(reportIds.stream().noneMatch(id -> atts.stream().anyMatch(f -> f.getTargetId().equalsIgnoreCase("T_LIMS_REPORT$" + id)))){
            this.addConstraintViolation(I18nHelper.getMessage("T_LIMS_REPORT.FILE_NOT_EMPTY"));
            return false;
        }
        return true;
    }
}
